package com.zyl.musicdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.zyl.musicdemo.R;

public class MyInputView extends FrameLayout {

    private int iconId;
    private boolean isPassword;
    private String inputHint;

    private View mView;
    private ImageView ivIcon, ivClose;
    private EditText etInput;

    public MyInputView(@NonNull Context context) {
        super(context);
        init(context, null);

    }

    public MyInputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyInputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyInputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) return;
        //获得
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.myInputView);
        iconId = array.getResourceId(R.styleable.myInputView_input_icon, R.mipmap.ic_launcher);
        isPassword = array.getBoolean(R.styleable.myInputView_is_password, false);
        inputHint = array.getString(R.styleable.myInputView_input_hint);
        //释放
        array.recycle();

        mView = LayoutInflater.from(context).inflate(R.layout.input_view, this, false);
        ivIcon = mView.findViewById(R.id.iv_input_icon);
        ivClose = mView.findViewById(R.id.iv_input_close);
        etInput = mView.findViewById(R.id.et_input);

        ivIcon.setImageResource(iconId);
        etInput.setHint(inputHint);
        etInput.setInputType(isPassword ? EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_PHONE);

        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etInput.setText("");
            }
        });
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && etInput.isFocused()) {
                    ivClose.setVisibility(VISIBLE);
                } else {
                    ivClose.setVisibility(GONE);
                }
            }
        });
        etInput.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus && !TextUtils.isEmpty(((EditText) v).getText().toString())) {
                    ivClose.setVisibility(VISIBLE);
                } else {
                    ivClose.setVisibility(GONE);
                }
            }
        });

        addView(mView);

    }

    /**
     * 返回输入数据
     */
    public String getInputString() {
        return etInput.getText().toString();
    }
}
